/**
 * Created by Richard Radlherr.
 * Date: 24.06.2006 21:07
 */

using System;
using System.Runtime.Serialization;

namespace TagTool.GraphicTypes
{
	[Serializable()]
	public class Color
	{
		private double a;
		private double r;
		private double g;
		private double b;
		
		public Color()
		{
		}
		
		public Color(double a, double r, double g, double b)
		{
			this.a = a;
			this.r = r;
			this.g = g;
			this.b = b;
		}
		
		public double A 
		{
			get { return a;	}
			set { a = value; }
		}
		
		public double R 
		{
			get { return r;	}
			set { r = value; }
		}

		public double G 
		{
			get { return g;	}
			set { g = value; }
		}
		
		public double B {
			get { return b;	}
			set { b = value; }
		}
		
		public override string ToString()
		{
			return String.Format("({0:n2}; {1:n2}; {2:n2}; {3:n})", a, r, g, b);
		}
		
		public Color fromHSV(double h, double s, double v, double a)
		{

			this.a = a;
			int h1 = (int)((h * 360) % 360);
			int segment = (int)(h1 / 60);
			double f = (h * 360.0 - (double)segment * 60.0) / 60.0;
			if (f > 1) f = 0;
			double p = v * (1.0 - s);
			double q = v * (1.0 - (s * f));
			double t = v * (1.0 - (s * ( 1.0 - f)));
			
			if (segment == 0)
			{
				r = v; g = t; b = p; // rot - rotgruen
			}
			if (segment == 1)
			{
				r = q; g = v; b = p; // rotgruen - gruen
			}
			if (segment == 2)
			{
				r = p; g = v; b = t; // gruen - gruenblau
			}
			if (segment == 3)
			{
				r = p; g = q; b = v; // gruenblau - blau
			}
			if (segment == 4)
			{ 
				r = t; g = p; b = v; // blau - blaurot
			}
			if (segment == 5)
			{
				r = v; g = p; b = q; // blaurot - rot
			}

			return this;
		}
		
		public static Color createHSV(double h, double s, double v, double a)
		{
			Color color = new Color();
			color.fromHSV(h, s, v, a);
			return color;
		}

	}
	
}
